function findPositions(ele) {
    var sid = $(ele).val();
    $.post("../admin/positions",
        {"sId": sid},
        function (data) {
            if (!$.isEmptyObject(data)) {
                $("#t_position").html("");
                for (var i = 0, n = data.length; i < n; i++) {
                    $("#t_position").append("<option value='" + data[i].id + "'>" + data[i].position + "</option>");
                }
            }
        });
}

function employees() {
    var status = $("#employee_status").val();
    var sId = $("#employee_sections").val();
    var pId = $("#employee_positions").val();
    var ele = $(".employee_table").find("#employee_tb");

    console.log("status = " + status + "---sId = " + sId + "---pId = " + pId);

    if (status == -1) {     //离职信息
        $.post("../employee/employees",
            {"status": status},
            function (data) {
                $(ele).html("");
                if ($.isEmptyObject(data))
                    $(ele).append("<tr><td colspan='5'>还没有相关信息消息</td></tr>");
                else {
                    for (var i = 0, n = data.length; i < n; i++) {
                        $(ele).append("<tr><td>" + data[i].id + "</td>" +
                            "<td  class='detail'><input type='hidden' name='userId' value='" + data[i].resume.user.id + "'>" + data[i].resume.realName + "</td>" +
                            "<td colspan='3'>其他</td></tr>");
                        var $cd = $("td[class=detail]");
                        $cd.css({"padding": "0 12px", "cursor": "pointer"});//包含属性值的选择器
                        $cd.css("color", "rgba(0,20,230,0.7)");//以属性值开始的选择器
                        $cd.hover(function () {     //包含属性值并以空格隔开的选择器
                            $(this).css("color", "rgba(0,200,60,0.7)");
                        }, function () {
                            $(this).css("color", "rgba(0,20,230,0.7)");
                        });

                        $cd.click(function () {
                            showEmployeeDetails(this, status);
                        });
                    }
                }
            });
    } else {    //在职信息
        $.post("../employee/employees",
            {"status": status, "sectionId": sId, "positionId": pId},
            function (data) {
                $(ele).html("");
                if ($.isEmptyObject(data))
                    $(ele).append("<tr><td colspan='5'>还没有消息</td></tr>");
                else {
                    for (var i = 0, n = data.length; i < n; i++) {
                        $(ele).append("<tr><td>" + data[i].id + "</td>" +
                            "<td  class='click_td detail'><input type='hidden' name='userId' value='" + data[i].resume.user.id + "'>" + data[i].resume.realName + "</td>" +
                            "<td  class='click_td transfer'><input type='hidden' name='userId' value='" + data[i].resume.user.id + "'>人事调动</td>" +
                            "<td  class='click_td attendance'><input type='hidden' name='userId' value='" + data[i].resume.user.id + "'>考勤</td>" +
                            // "<td  class='click_td salary'><input type='hidden' name='userId' value='" + data[i].resume.user.id + "'>工资发放</td>" +
                            "<td  class='click_td expel'><input type='hidden' name='userId' value='" + data[i].resume.user.id + "'>" +
                            "<input type='hidden' name='resumeId' value='" + data[i].resume.id + "'>开除</td></tr>");
                        $("td[class*=click_td]").css({"padding": "0 12px", "cursor": "pointer"});//包含属性值的选择器

                        $("td[class^=click_td]").css("color", "rgba(0,20,230,0.7)");//以属性值开始的选择器

                        $("td[class~=click_td]").hover(function () {     //包含属性值并以空格隔开的选择器
                            $(this).css("color", "rgba(0,200,60,0.7)");
                        }, function () {
                            $(this).css("color", "rgba(0,20,230,0.7)");
                        });

                        $("td[class~=detail]").click(function () {
                            showEmployeeDetails(this, status);
                        });

                        $("td[class~=transfer]").click(function () {
                            showEmployeeTransfer(this);
                        });

                        $("td[class~=attendance]").click(function () {
                            showAttendance(this);
                        });

                        $("td[class~=salary]").click(function () {
                            var now = new Date().getDate();
                            if (now < 20)
                                alert("工资每月20号以后统一发放！");
                        });

                        $("td[class~=expel]").click(function () {
                            expelEmployee(this);
                        });
                    }
                }
            });
    }
}

function expelEmployee(ele) {
    var uId = $(ele).find("input[name=userId]").val();
    var rId = $(ele).find("input[name=resumeId]").val();
    if (confirm("您确定要开除员工吗？")) {
        console.log("uId = " + uId + "-----rId = " + rId);
        $.post("../admin/expelEmployee",
            {"uId": uId, "rId": rId},
            function (data) {
                if (data === 1) {
                    alert("操作成功,请刷新页面！");
                    $(ele).parent().html("<td colspan='6'>没有相关消息！</td>");
                } else {
                    alert("操作失败，请重试！")
                }
            });
    }
}

function showAttendance(ele) {
    var uId = $(ele).find("input[name=userId]").val();

    selectYMD($("#year"), $("#month"), $("#day"));

    $("#a_table").find("input[name=userId]").val(uId);
    $("#a_table").find("tbody").html("<tr><td colspan='6'>支持按月份和日期查询，请操作！</td></td></tr>");

    $("#admin_main_div").children().hide();
    $("#employee_attendance").show();
}

function findAttendance(ele) {
    var uId = $("#a_table").find("input[name=userId]").val();
    var y = $("#year").val();
    var m = $("#month").val();
    var d = $("#day").val();

    var $tb = $("#a_table").find("tbody");
    // console.log("userId = " + uId + "---y = " + y + "---m = " + m + "---d = " + d);
    $.post("../attendance/findAttendances",
        {"userId": uId, "year": y, "month": m, "day": d},
        function (data) {
            if ($.isEmptyObject(data)) {
                alert("还没有相关考勤信息！");
            } else {
                $tb.html("");
                console.log("data = " + data[0]);
                for (var i = 0, n = data.length; i < n; i++) {
                    var ot = data[i].offTime;
                    $tb.append("<tr><td>" + data[i].resume.realName + "</td>" +
                        "<td>" + new Date(data[i].goTime).Format("yyyy-MM-dd hh:mm:ss") + "</td>" +
                        "<td>" + (ot === null ? "未签退" : new Date(ot).Format("yyyy-MM-dd hh:mm:ss")) + "</td>" +
                        "<td>" + data[i].beLateTimes + "分钟</td>" +
                        "<td>" + data[i].leaveEarlyTimes + "分钟</td>" +
                        "<td>" + data[i].extraWorkTimes + "小时</td></tr>");
                }
                $tb.find("td").css({"text-align": "center", "padding": "3px 0px"});
            }
        });
    $.post("../attendance/getOnJobDays",
        {"userId": uId, "year": y, "month": m},
        function (data) {
            $("#on_job_days").html(data);
        });
}

function showEmployeeTransfer(ele) {
    var uId = $(ele).find("input[name=userId]").val();
    var $uIdInput = $("#t_table").find("input[name=userId]");
    $uIdInput.val(uId);
    // console.log("$uIdInput.val() = " + $uIdInput.val());
    $("#admin_main_div").children(":not('#employee_all_div')").hide();
    $("#employee_transfer").show();
}

function transferSubmit(ele) {
    var uId = $("#t_table").find("input[name=userId]").val();
    var sId = $("#t_table").find("#t_section").val();
    var pId = $("#t_table").find("#t_position").val();
    if (sId === "") {
        alert("请选择调动的部门！");
        return;
    }
    if (pId === "") {
        alert("请选择调动的职位！");
        return;
    }
    $.post("../admin/getResume",
        {"uId": uId},
        function (data) {
            console.log("data.position.id = " + data.position.id + " ***********pId = " + pId);
            if (data.position.id == pId)
                alert("该职工已在该职位就职，无需调动！");
            else {
                $.post("../admin/updateResume",
                    {"user.id": uId, "position.id": pId},
                    function (data) {
                        if (data === 1) {
                            alert("消息发布成功！");
                            $("#employee_transfer").hide();
                        } else
                            alert("消息发布失败！");
                    });
            }
        });
}

function showEmployeeDetails(ele, status) {
    var uId = $(ele).find("input[name=userId]").val();
    console.log("resumeId = " + uId);
    $.post("../admin/getEmployee",
        {"userId": uId},
        function (data) {
            var ed = new Date(data.entryDate);
            $("#detail_name").html(data.resume.realName);
            $("#detail_age").html(data.resume.age);
            $("#detail_sex").html(data.resume.sex);
            $("#detail_phone").html(data.resume.user.phone);
            $("#detail_education").html(data.resume.education.education);
            $("#detail_email").html(data.resume.email);
            $("#detail_position").html(data.resume.position.section.section + " ").append(" " + data.resume.position.position);
            $("#detail_entry_date").html(ed.getFullYear() + "/" + (ed.getMonth() + 1) + "/" + ed.getDate());
            $("#detail_resume_id").val(data.resume.id);
            if (status !== "1") {
                $("#detail_reward").hide();
                $("#detail_punishment").hide();
            } else {
                $("#detail_reward").show();
                $("#detail_punishment").show();
            }
            $("#employee_details").siblings().hide();
            $("#employee_details").show();
        });
}

function clickReward() {
    $("#r_resume_id").val($("#detail_resume_id").val());
    $("#employee_rp").show();
    $("#employee_rp").find("#p_table").hide();
    $("#employee_rp").find("#r_table").show();
}

function clickPunishment() {
    $("#p_resume_id").val($("#detail_resume_id").val());
    $("#employee_rp").show();
    $("#employee_rp").find("#r_table").hide();
    $("#employee_rp").find("#p_table").show();
}

function rpSubmit(ele) {
    var rId = $(ele).find("[name=rId]").val();
    var cause = $(ele).find("[name=cause]").val();
    var date = $(ele).find("[name=date]").val();
    var money = $(ele).find("[name=money]").val();
    var rop = $(ele).find("[name=r_or_p]").val();
    if (cause === '') {
        alert("请输入奖惩原因！");
        return;
    }
    if (date === '') {
        alert("请选择时间！");
        return;
    }
    if (money === '') {
        alert("请选择奖惩金额！");
        return;
    }
    // console.log("rId = " + rId + "*****cause = " + cause + "*****date = " + date + "*****money = " + money + "*****rop = " + rop);
    $.post("../admin/addRP",
        {"resume.id": rId, "cause": cause, "rpDate": date, "money": money, "rewardOrPunishment": rop},
        function (data) {
            if (data === 1) {
                alert("消息发布成功！");
                $("#employee_rp").hide();
            } else
                alert("消息发布失败！");
        });
}

function chooseSection() {
    var status = $("#employee_status").val();

    if (status === "1")
        $("#employee_sections").show();
    else
        $("#employee_sections").hide();

    employees();
}

function choosePosition() {
    var sectionId = $("#employee_sections").val();
    var $sp = $("#employee_positions");

    if (sectionId !== "") {
        $.post("../admin/positions",
            {"sId": sectionId},
            function (data) {
                if ($.isEmptyObject(data)) {
                    $sp.html("").append("<option value=''>职位</option>");
                } else {
                    $sp.html("<option value=''>职位</option>");
                    for (var i = 0, n = data.length; i < n; i++) {
                        $sp.append("<option value='" + data[i].id + "'>" + data[i].position + "</option>")
                    }
                }
            });
        $sp.show();
    } else
        $("#employee_positions").hide();

    employees();
}